package com.avergel.metaserver.filtro;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FiltroRedireccionLogin implements Filter {
	ResourceBundle bundleConfig = ResourceBundle.getBundle("config");	
	@SuppressWarnings("unused")
	private FilterConfig config;

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
			ServletException {
		if (((HttpServletRequest) req).getSession().getAttribute("usuario") != null) {
			((HttpServletResponse) resp).sendRedirect("/"+ bundleConfig.getString("nombreProyecto") + "/app/principal.xhtml");
		} else {
			chain.doFilter(req, resp);
		}
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}

	public void destroy() {
		config = null;
	}
}